package pers.gxm.javaweb;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MyHttpServer {
    private final int port = 8080;

    public void waiting(){
        try {
            ServerSocket serverSocket = new ServerSocket(port);
            while(true){
//                TODO 获取连接对象
                Socket accept = serverSocket.accept();
//                System.out.println("accept = " + accept);
//                TODO 获取连接对象的输入流
                InputStream is = accept.getInputStream();
//                 TODO 将请求转换为字符串
                MyHttpRequest request = new MyHttpRequest(is);
                String str = request.parse();
//                System.out.println(str);
//                 TODO 获取请求路径
                String uri = request.getPath(str);
//                System.out.println("uri = " + uri);
                OutputStream os = accept.getOutputStream();
//                TODO 返回响应
                MyHttpResponse myHttpResponse = new MyHttpResponse(os);
                myHttpResponse.sendDirect(uri);

            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
